package com.cnswhy.cloud.server.merchant.controller.shop;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.request.PageParams;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.mybatis.conditions.query.QueryWrap;
import com.cnswhy.cloud.common.core.utils.DateUtils;
import com.cnswhy.cloud.server.core.dto.ShopSpecPageDTO;
import com.cnswhy.cloud.server.core.dto.ShopSpecSaveDTO;
import com.cnswhy.cloud.server.core.dto.ShopSpecUpdateDTO;
import com.cnswhy.cloud.server.core.entity.ShopSpec;
import com.cnswhy.cloud.server.core.service.ShopSpecService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 规格表
 * </p>
 *
 * @author cnswhy
 * @date 2020-11-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopSpec")
@Api(value = "ShopSpec", tags = "规格表")
//@PreAuth(replace = "shopSpec:")
public class ShopSpecController extends SuperController<ShopSpecService, Long, ShopSpec, ShopSpecPageDTO, ShopSpecSaveDTO, ShopSpecUpdateDTO> {

    @Override
    public QueryWrap<ShopSpec> handlerWrapper(ShopSpec model, PageParams<ShopSpecPageDTO> params) {
        QueryWrap<ShopSpec> wrapper = model == null ? Wraps.q() : Wraps.q(model);

        if (CollUtil.isNotEmpty(params.getMap())) {
            Map<String, String> map = params.getMap();
            //拼装区间
            for (Map.Entry<String, String> field : map.entrySet()) {
                String key = field.getKey();
                String value = field.getValue();
                if (StrUtil.isEmpty(value)) {
                    continue;
                }
                if (key.endsWith("_st")) {
                    String beanField = StrUtil.subBefore(key, "_st", true);
                    wrapper.ge(getDbField(beanField, getEntityClass()), DateUtils.getStartTime(value));
                }
                if (key.endsWith("_ed")) {
                    String beanField = StrUtil.subBefore(key, "_ed", true);
                    wrapper.le(getDbField(beanField, getEntityClass()), DateUtils.getEndTime(value));
                }
            }
        }
        wrapper.orderByAsc("sort_order");
        return wrapper;
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ShopSpec> shopSpecList = list.stream().map((map) -> {
            ShopSpec shopSpec = ShopSpec.builder().build();
            return shopSpec;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopSpecList));
    }
}
